package com.sywb.chuangyebao.view.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.widget.ColorTrackTabLayout;

import org.bining.footstone.adapter.BasePagerFragmentAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 创业万里行
 */
public class CywlxFragment extends BaseStatisticsFragment {

    ColorTrackTabLayout tlIndex;
    ImageView ivBack;
    ViewPager viewPager;
    ImageView ivSearch;

    private List<String> mTitleDatas;
    private List<Fragment> mFragments;
    private FragmentPagerAdapter fragmentPagerAdapter;

    /**
     * Fragment 实例
     */
    public static CywlxFragment newInstance(Object... pramars) {
        CywlxFragment fragment = new CywlxFragment();
        fragment.setArguments(getBundle(pramars));
        return fragment;
    }

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.activity_user_friends;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        super.initView(view, bundle);
        tlIndex = (ColorTrackTabLayout) getView(R.id.tl_index);
        ivBack = (ImageView) getView(R.id.iv_back);
        viewPager = (ViewPager) getView(R.id.view_pager);
        ivSearch = (ImageView) getView(R.id.iv_search);

        ivBack.setVisibility(View.GONE);
    }

    @Override
    public void initPresenter() {
        mTitleDatas = new ArrayList<>();
        mFragments = new ArrayList<>();
        mTitleDatas.add("推荐");
        mFragments.add(RecommendFragment.newInstance(1));
        mTitleDatas.add("文章");
        mFragments.add(NewsFragment.newInstance(1234567));
        mTitleDatas.add("视频");
        mFragments.add(VideoFragment.newInstance(2, 1234567));
//        mTitleDatas.add("合作");
//        mFragments.add(EnrollFragment.newInstance());

        fragmentPagerAdapter = new MyPagerFragmentAdapter(getChildFragmentManager(), mFragments, mTitleDatas);
        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.setOffscreenPageLimit(mTitleDatas.size());
        int padding = mActivity.getResources().getDimensionPixelSize(R.dimen.dp_16);
        viewPager.setCurrentItem(0);
        tlIndex.setTabPaddingLeftAndRight(padding, padding);
        tlIndex.setupWithViewPager(viewPager);

        //隐藏指示器
        tlIndex.setSelectedTabIndicatorHeight(6);
    }

    class MyPagerFragmentAdapter extends BasePagerFragmentAdapter {

        private List<Fragment> fragments;
        private List<String> channels;

        public MyPagerFragmentAdapter(FragmentManager fragmentManager, List<Fragment> fragments, List<String> channels) {
            super(fragmentManager, fragments);
            this.fragments = fragments;
            this.channels = channels;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return channels.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return channels == null ? "" : channels.get(position);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }

}
